﻿using System;
using System.Collections.Generic;
using TopShooter.Items;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;

namespace TopShooter.Units
{
	public class Infantry : Unit
	{
		public Infantry(int team, Weapon weapon)
			: base(team)
		{
			MovementSpeed = 200;
			Health = 100;
			Name = "Infantry";
			Weapon = weapon;
		}

		protected override void OnAIupdate()
		{
			base.OnAIupdate();

			if (CommandQueue.Count > 0)
			{
				if (CurrentCommand.WasUserIssued)
					return;
				else { } // reconsider!
			}

			Unit closestUnit = null;
			float closestDistance = 300;

			foreach (Entity e in Scene.Entities)
			{
				Unit u = e as Unit;
				if (u != null)
				{
					if (u.TeamID == TeamID || e.IsInvulnerable)
						continue;

					float dx = u.Location.X - Location.X;
					float dy = u.Location.Y - Location.Y;
					float dist = (float)Math.Sqrt((dx * dx) + (dy * dy));

					if (dist < closestDistance)
					{
						closestDistance = dist;
						closestUnit = u;
					}
				}
			}
			if (closestUnit != null)
			{
				UnitCommand c = new UnitCommand.Attack(closestUnit);
				c.WasUserIssued = false;
				AddCommand(c);
			}
		}

		protected override void OnLoadContent()
		{
			
			Weapon.SetOwner(this);
			Armor = new Armor(20, 40);
			Armor.SetOwner(this);
			_texture = Scene.Content.Load<Texture2D>("Entities/PlayerCircle");
			_origin = new Vector2(_texture.Width / 2.0f, _texture.Height / 2.0f);
			_destroyedSound = Scene.Content.Load<SoundEffect>("Sounds/PlayerKilled");

			BoundingRadius = 16;
		}

	}
}
